package com.gzs.support.fasfdfs;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.TrackerGroup;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;


/**
 * FastDFS
 * @author 987
 *
 */
public class FastDFSClientFactory  implements FactoryBean<ClientGlobal>, InitializingBean{
	private Resource addressConfig;
	private String addressKeyPrefix;
	private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");  
	
	public static int g_connect_timeout;
	public static int g_network_timeout;
	public static String g_charset;
	public static int g_tracker_http_port;
	public static boolean g_anti_steal_token;
	public static String g_secret_key;
	public static TrackerGroup g_tracker_group;
	public static final int DEFAULT_CONNECT_TIMEOUT = 5;
	public static final int DEFAULT_NETWORK_TIMEOUT = 30;
	  
	public static Socket getSocket(String ip_addr, int port)
			throws IOException{
		Socket sock = new Socket();
		sock.setSoTimeout(g_network_timeout);
		sock.connect(new InetSocketAddress(ip_addr, port), g_connect_timeout);
		return sock;
	}
  
	public static Socket getSocket(InetSocketAddress addr)
			throws IOException{
	    Socket sock = new Socket();
	    sock.setSoTimeout(g_network_timeout);
	    sock.connect(addr, g_connect_timeout);
	    return sock;
	}
  
 	public static int getG_connect_timeout(){
 		return g_connect_timeout;
 	}
  
 	public static void setG_connect_timeout(int connect_timeout){
 		g_connect_timeout = connect_timeout;
 	}
  
 	public static int getG_network_timeout(){
 		return g_network_timeout;
 	}
  
 	public static void setG_network_timeout(int network_timeout){
 		g_network_timeout = network_timeout;
 	}
  
 	public static String getG_charset(){
 		return g_charset;
 	}
  
 	public static void setG_charset(String charset){
 		g_charset = charset;
 	}
  
 	public static int getG_tracker_http_port(){
 		return g_tracker_http_port;
 	}
  
 	public static void setG_tracker_http_port(int tracker_http_port){
 		g_tracker_http_port = tracker_http_port;
 	}
  
 	public static boolean getG_anti_steal_token(){
 		return g_anti_steal_token;
 	}
  
 	public static boolean isG_anti_steal_token(){
 		return g_anti_steal_token;
 	}
  
 	public static void setG_anti_steal_token(boolean anti_steal_token){
 		g_anti_steal_token = anti_steal_token;
 	}
  
 	public static String getG_secret_key(){
 		return g_secret_key;
 	}
  
 	public static void setG_secret_key(String secret_key){
 		g_secret_key = secret_key;
 	}
  
 	public static TrackerGroup getG_tracker_group(){
 		return g_tracker_group;
 	}
  
 	public static void setG_tracker_group(TrackerGroup tracker_group){
 		g_tracker_group = tracker_group;
 	}
 	
 	public Resource getAddressConfig() {
		return addressConfig;
	}

	public void setAddressConfig(Resource addressConfig) {
		this.addressConfig = addressConfig;
	}

	public String getAddressKeyPrefix() {
		return addressKeyPrefix;
	}

	public void setAddressKeyPrefix(String addressKeyPrefix) {
		this.addressKeyPrefix = addressKeyPrefix;
	}
 	
 	private InetSocketAddress[] propertiesSet() throws Exception {  
        try {  
            Properties prop = new Properties();  
            prop.load(this.addressConfig.getInputStream());  
  
            Set<InetSocketAddress> haps = new HashSet<InetSocketAddress>();
            for (Object key : prop.keySet()) {  
                if (!((String) key).startsWith(addressKeyPrefix)) {  
                    continue;  
                }  
  
                String val = (String) prop.get(key);  
                boolean isIpPort = p.matcher(val).matches();  
                if (!isIpPort) {  
                    throw new IllegalArgumentException("ip 或 port 不合法");  
                }  
                String[] ipAndPort = val.split(":");  
  
                InetSocketAddress hap = new InetSocketAddress(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1].trim()));  
                haps.add(hap);
            } 
            
            InetSocketAddress[] result = new InetSocketAddress[haps.size()];
            int i = 0; 
            for (InetSocketAddress inetSocketAddress : haps) {
            	result[i] = inetSocketAddress;
            	i++;
			}
  
            return result;  
        } catch (IllegalArgumentException ex) {  
            throw ex;  
        } catch (Exception ex) {  
            throw new Exception("解析 fastdfs 配置文件失败", ex);  
        }  
    }  

//	@Override
	public void afterPropertiesSet() throws Exception {
		InetSocketAddress[] tracker_servers = propertiesSet();
	    g_tracker_group = new TrackerGroup(tracker_servers);
	}

//	@Override
	public boolean isSingleton() {
		return true;  
	}
	
//	@Override
	public ClientGlobal getObject() throws Exception {
		return null;
	}

//	@Override
	public Class<?> getObjectType() {
		return null;
	}

}

